You can specify property validations using the NHibernate Designer. Select the property to be validated, and choose one or more of the following validations:

Option Description
Validate Length Validate the length of the property value (minimum and/or maximum length. See below for format.
Validate Presence Validate that the property value is not null and/or not empty
Validate Email Validate that the property value is a well-formed email address
Validate Value Validate that the property values falls within a specified range. See below for format.
Validate Date Validate that the property value is a date that falls in the past (or future)
Validate Pattern Validate that the property value conforms to the specified regular expression

For additional options including custom error messages, open the NHibernate Model Explorer (View > Other Windows > NHibernate Model), expand the tree view to show the property, expand the Validations folder, select the validation and enter its Custom Error through the properties grid.

Validations are implemented using NHibernate.Validator. See the NHibernate.Validator documentation for more information. Note that not all validation options map directly to NHibernate.Validator attributes – for example Validate Presence maps to different attributes depending on the selection.

Range Notation for Length and Value Validations

The length and value validations allow you to specify a minimum, maximum or range using the following notation:

Specification Notation
Minimum >= min
Maximum <= max
Range min – max

For example, to specify that the maximum length of a string is 80, set Validate Length to <= 80, or to specify that an integer property must be in the range 0 to 130, set Validate Value to 0 – 130.

Custom Validations

To apply a validation which is not shown in the Properties grid, open the NHibernate Model Explorer (View > Other Windows > NHibernate Model), expand the tree view to show the property, then right-click the property and choose Add New Custom Validation. Then use the Properties grid to enter the validation attribute as you want it to appear in the generated code (excluding the attribute brackets), e.g. MyValidation(123, Prop=456).